
package com.zsrt.book.util;

import java.io.DataOutput;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import com.zsrt.book.dao.BookManagerDao;
import com.zsrt.book.dao.UserBookDao;
import com.zsrt.book.dao.impl.BookManagerDaoImpl;
import com.zsrt.book.dao.impl.UserBookDaoImpl;
import com.zsrt.book.entity.BookEntity;
import com.zsrt.book.entity.EmpolyeeEntity;


public class CommonUtil {

	//全局信息
	List<BookEntity> books = BookUtil.init();
	List<EmpolyeeEntity> empolyees = EmpolyeeUtil.initEmployee();
	public static String userConstants="src/com/zsrt/book/user.properties";
	public static String employeeConstants="src/com/zsrt/book/employee.xml";
	public static String bookConstants="src/com/zsrt/book/book.xml";
	public static BookManagerDao dao = new BookManagerDaoImpl();
	
	/**
	 * 主界面
	 */
	public static void firstPage(){
		printZSRT();
		System.err.println("\t  1.管理员入口");
		System.err.println("\t  2.员工入口");
		System.err.println("\t  3.退出");
		System.err.println("请输入操作数：");
	}
	
	/**
	 * 管理员界面
	 */
	public static void adminPage(){
		
		printZSRT();
		System.err.println("\t  1.添加图书");
		System.err.println("\t  2.修改图书");
		System.err.println("\t  3.删除图书");
		System.err.println("\t  4.显示图书");
		System.err.println("\t  5.退出");
		System.err.println("请输入操作数：");
	}

	/**
	 * 打印中深软通
	 */
	private static void printZSRT() {
		for(int hang = 1;hang <=5;hang++){
			System.err.print("*");
			if( hang ==1 || hang == 5){
				for(int i=1;i<=30;i++){
					System.err.print("*");
				}
			}else if(hang==3){
				System.err.print("\t 中深软通图书借阅系统 \t\t");
			}else{
				
				for(int i=1;i<=30;i++){
					System.err.print(" ");
				}	
			}
			System.err.print("*");
			System.err.println();		
		}
	}
	
	/**
	 * 用户界面
	 */
	public static void userPage(){
		printZSRT();
		System.err.println("\t  1.借阅");
		System.err.println("\t  2.归还");
		System.err.println("\t  3.退出");
		System.err.println("请输入操作数：");
	}
	
	/**
	 * 美化打印效果
	 * @return
	 */
	public static OutputFormat getOutputFormat(){
		//上线
		OutputFormat format=OutputFormat.createCompactFormat();
		//2 设置XML编码方式,即是用指定的编码方式保存XML文档到字符串(String),这里也可以指定为GBK或是ISO8859-1
		format.setEncoding("utf-8");
		//设置是否缩进
		format.setIndent(true);   
		// 以四个空格方式实现缩进
		format.setIndent("\t");   
		// 设置是否换行
		format.setNewlines(true); 

		return format;
	}
	
	/**
	 * 把xml文档打印到控制台
	 * @param document
	 */
	public static void print(Document document) throws Exception{
		
		//stringWriter字符串是用来保存XML文档的
		StringWriter stringWriter = new StringWriter();
		//xmlWriter是用来把XML文档写入字符串的工具
		XMLWriter xmlWriter = new XMLWriter(stringWriter, getOutputFormat());

		// 把创建好的XML文档写入字符串
		xmlWriter.write(document);

		// 打印字符串,即是XML文档
		System.err.println(stringWriter);

		xmlWriter.close();
	}
	
	public static void  add(Document document,String file) throws Exception{
		
		PrintWriter pw = new PrintWriter(file);
		
		OutputFormat format = getOutputFormat();
        XMLWriter xw = new XMLWriter(pw,format);  
        xw.write(document);  
        xw.flush();  
        xw.close();  
	}
	/**
	 * 管理员登录以及选择
	 * @param scanner
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	public static void adminLoginPage(Scanner scanner) throws FileNotFoundException, IOException{
		
		printZSRT();
		Properties pro = new Properties();
		pro.load(new FileReader(userConstants));
		String un = pro.getProperty("username");
		String pw = pro.getProperty("password");
		
		System.err.println("请输入管理员账户");
		String un1 = scanner.next();
		System.err.println("请输入管理员密码");
		String pw1 = scanner.next();
		
			
		while(true){
			if(un.equals(un1) && pw.equals(pw1)){
				System.err.println("登陆成功");
				adminPage();
				int n =scanner.nextInt();
			
				if(n==1){
					//操作数为1  添加图书
					boolean b = dao.addBook(scanner, bookConstants);
					System.err.println(b);
				}else if(n==2){
					//操作数为2 修改图书
					System.err.println("请输入你想修改的图书编号");
					int id = scanner.nextInt();
					boolean b = dao.updateBook(scanner, id , bookConstants);
					System.err.println(b);
					
				}else if(n==3){
					// 删除图书
					System.err.println("请输入你想删除的图书编号");
					int id =scanner.nextInt();
					boolean b = dao.deleteBook(id, bookConstants);
					System.err.println(b);
			
				}else if(n==4){
					//操作数为4   显示图书
					
					dao.showBooks(bookConstants);
				}else{
			
					System.err.println("退出管理员登录");
					break;
				}
			}else{
				System.err.println("登陆失败,用户名或密码错误,您已被强制退出...");
				break;
			}
		}	
	}
	
	public static void userLoginPage(Scanner scanner){
		UserBookDao ubd = new UserBookDaoImpl();
		printZSRT();
		File file = new File(employeeConstants);
		SAXReader reader = new SAXReader();
		Document document = null;
		try {
			document =reader.read(file);
			Element root = document.getRootElement();
			System.err.println("请输入借书证号码");
			String un1 = scanner.next();
			System.err.println("请输入借书证密码");
			String pw1 = scanner.next();
			
			List<Element> nodes = root.selectNodes("//employee");
			//遍历节点看是否在用户表中存在
			for(Element e: nodes){
				String num = e.elementText("cardNum");
				String password = e.elementText("cardPass");
				if(un1.equals(num) && pw1.equals(password)){
					System.err.println("登陆成功");	
					while(true){
						userPage();
						int n=scanner.nextInt();
						if(n==1){
							//借阅图书
							boolean b = ubd.borrowBook(scanner, bookConstants, un1, employeeConstants);
							System.err.println(b);
							
						}else if(n==2){
							//归还图书
							
							boolean b = ubd.revertBook(scanner, bookConstants, un1, employeeConstants);
							System.err.println(b);
							
						}else{
							//退出
							System.err.println("退出用户登录");
							break;
							
						}
					}
					
				}
			}
			//如果在用户表中不存在则登录失败
			System.err.println("登录失败");
			
		} catch (DocumentException e) {
			e.printStackTrace();
		}	
	}
}
